package typejump.objects;

import typejump.AnimationManager;
import typejump.engine.Animation;

public class AttachableSparkles extends Sparkles {
	
	public static final int ATTACH_LEFT = 1;
	public static final int ATTACH_RIGHT = 2;
	public static final int ATTACH_TOP = 4;
	public static final int ATTACH_BOTTOM = 8;
	public static final int ATTACH_CENTERX = 16;
	public static final int ATTACH_CENTERY = 32;
	
	public static final int ATTACH_CENTER = ATTACH_CENTERX | ATTACH_CENTERY;
	public static final int ATTACH_CENTER_BOTTOM = ATTACH_CENTERX | ATTACH_BOTTOM;

	private GameObject host;
	private int attach;
	
	public AttachableSparkles(GameObject host, Animation anim, double length, int attach) {
		super(Math.max(host.width(), 1), 0, 0, 0, 50, 100, 1000);
		setAnimation(anim);
		setScale(0.3);
		setLifetime(0.5);
		setLength(length);
		this.host = host;
		
		setAttach(attach);
		
		update(0);
	}
	public AttachableSparkles(GameObject host, Animation anim, double length) {
		this(host, anim, length, ATTACH_CENTER_BOTTOM);
	}
	public AttachableSparkles(GameObject host, Animation anim) {
		this(host, anim, 0, ATTACH_CENTER_BOTTOM);
	}
	
	public void setAttach(int attach) {
		this.attach = attach;
		if (isAttach(ATTACH_CENTERX)) {
			centerX(host.centerX());
			setRangeX(host.width());
		} else if (isAttach(ATTACH_LEFT)) {
			centerX(host.left());
			setRangeX(0);
		} else if (isAttach(ATTACH_RIGHT)) {
			centerX(host.right());
			setRangeX(0);
		}
		
		if (isAttach(ATTACH_CENTERY)) {
			centerY(host.centerY());
			setRangeY(host.height());
		} else if (isAttach(ATTACH_TOP)) {
			centerY(host.top());
			setRangeY(0);
		} else if (isAttach(ATTACH_BOTTOM)) {
			centerY(host.bottom());
			setRangeY(0);
		}
	}
	
	public void setLength(double length) {
		if (length > 0) {
			yspeedmax = length/sparkleLife;
			yspeedmin = yspeedmax/3;
		} else {
			yspeedmin = yspeedmax = 0;
		}
	}
	
	private boolean isAttach(int att) {
		return (att & attach) != 0;
	}
	
	public void update(double time) {
		super.update(time);
		
		if (host == null || host.doremove) {
			stopSparkling();
			return;
		}
		
		if (isAttach(ATTACH_CENTERX)) {
			centerX(host.centerX());
		} else if (isAttach(ATTACH_LEFT)) {
			centerX(host.left());
		} else if (isAttach(ATTACH_RIGHT)) {
			centerX(host.right());
		}
		
		if (isAttach(ATTACH_CENTERY)) {
			centerY(host.centerY());
		} else if (isAttach(ATTACH_TOP)) {
			centerY(host.top());
		} else if (isAttach(ATTACH_BOTTOM)) {
			centerY(host.bottom());
		}
	}
}
